package com.jeff.tianti.controller;

import com.jeff.tianti.cms.entity.Area;
import com.jeff.tianti.cms.entity.Resume;
import com.jeff.tianti.cms.service.AreaService;
import com.jeff.tianti.cms.service.ResumeService;
import com.jeff.tianti.common.dto.AjaxResult;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@CrossOrigin(maxAge = 3600)
@RestController
@RequestMapping("/resume")//简历投递表
public class ResumeController {
    @Autowired
    private ResumeService resumeService;
    @Autowired
    private AreaService areaService;

    /**
     * @Ceator:liuYI
     * @Describe:人才天地用户发布简历
     */
    @RequestMapping(value = "/save", method = RequestMethod.POST)
    public AjaxResult saveResume(@RequestBody Resume newResume) {
        String id = newResume.getId();
        if (StringUtils.isNotBlank(id)) {//修改
            Resume oldResume = resumeService.find(id);
            Integer oldAreaId = oldResume.getAreaId();
            Integer newAreaId = newResume.getAreaId();
//            更新经纬度
            if (!oldAreaId.equals(newAreaId)) {
                Area area = areaService.find(newAreaId);
                newResume.setLongitud(area.getLongitud());
                newResume.setLatitude(area.getLatitude());
            }
            resumeService.update(newResume);
        } else { //添加
            int count = resumeService.findCountByUid(newResume.getUid());
            if (count >= 3) {
                throw new RuntimeException("每个用户最多发布3条简历");
            }
            Area area = areaService.find(newResume.getAreaId());
            newResume.setLatitude(area.getLatitude());
            newResume.setLongitud(area.getLongitud());
            resumeService.save(newResume);
        }
        return AjaxResult.setSuccessData(null);
    }

    /**
     * @Ceator:liuYI
     * @Describe:用户删除简历
     */
    @RequestMapping(value = "/del", method = RequestMethod.GET)
    public AjaxResult delResume(@RequestParam("id") String id, @RequestParam("uid") String uid) {
        if (StringUtils.isNotBlank(id)) {
            throw new RuntimeException("id:参数为空");
        }
        Resume resume = resumeService.find(id);
        if (!uid.equals(resume.getUid())) {
            throw new RuntimeException("非法操作");
        }
        resumeService.delete(id);
        return AjaxResult.setSuccessData(null);
    }
}
